/**   
 * @ClassName: SysLogAspect.java 
 * @Package com.vrv.aop 
 * @author LQ   
 * @date 2017年5月12日 下午6:05:06 
 * @version V1.0
 */
package com.vrv.aop;

import java.lang.reflect.Method;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.vrv.common.annotation.SysLog;
import com.vrv.log.model.Log;
import com.vrv.log.service.LogService;
import com.vrv.util.HttpContextUtils;
import com.vrv.util.IPUtils;
import com.vrv.util.ShiroUtils;

/** 
 * 系统日志，切面处理类
 * @author LQ 
 */
@Aspect
@Component
public class SysLogAspect {
    
    @Autowired
    private LogService logService;
    
    @Pointcut("@annotation(com.vrv.common.annotation.SysLog)") 
    public void logPointCut() { 
        
    }
    
    @Before("logPointCut()")
    public void saveSysLog(JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        
        Log log = new Log();
        SysLog syslog = method.getAnnotation(SysLog.class);
        if(syslog != null){
            //注解上的描述 
            log.setOperation(syslog.value());
        }
        
        //请求的方法名
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = signature.getName();
        log.setMethod(className + "." + methodName + "()");
        
        //请求的参数
        Object[] args = joinPoint.getArgs();
        String params = JSON.toJSONString(args[0]);
        log.setParams(params);
        
        //获取request
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        //设置IP地址
        log.setIp(IPUtils.getIpAddr(request));
        //用户编号
        log.setUserCode(ShiroUtils.getUserId());
        
        log.setCreateDate(new Date());
        //保存系统日志
        logService.add(log);
    }
    
}
